package com.molyfun.parents.zhibo;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;

import com.google.gson.JsonObject;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.im.MyTIMMsgListener;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.ZhiboRoomResponse;
import com.molyfun.parents.zhibo.im.event.EventHandler;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * This demo shows how to use PLMediaPlayer API playing video stream
 */
public class ZhiboActivity extends ZhiboBaseActivity {

    private static final String FLAG_ZHIBO_ROOM_ID = "zhibo_room_id";
    private static final String FLAG_ZHIBO_ROOM_NAME = "zhibo_room_name";
    private static final String FLAG_ZHIBO_ROOM_TEACHER_NAME = "zhibo_room_teacher_name";
    private static final String FLAG_ZHIBO_ROOM_USER_COUNT = "zhibo_room_user_count";

    private static final String FLAG_ZHIBO_ROOM_CLASS_ROOM = "zhibo_class_room";
    public static final String FLAG_ZHIBO_ROOM_SUBJECT_ID = "zhibo_subject_id";

    private Handler mHandler = new Handler();

    private String mCourserelid;


    public static void startActivity(Context context, String zhiboId, String zhiboName, String subjectId){
        Intent intent = new Intent();
        intent.setClass(context, ZhiboActivity.class);
        intent.putExtra(FLAG_ZHIBO_ROOM_ID, zhiboId);
        intent.putExtra(FLAG_ZHIBO_ROOM_NAME, zhiboName);
        intent.putExtra(FLAG_ZHIBO_ROOM_SUBJECT_ID, subjectId);
//        intent.putExtra(FLAG_ZHIBO_ROOM_TEACHER_NAME, teacherName)
//        intent.putExtra(FLAG_ZHIBO_ROOM_USER_COUNT, userCount)
        context.startActivity(intent);
    }
//
//    public static void startActivity(Context context, String zhiboId, String zhiboName, String teacherName, String userCount){
//        Intent intent = new Intent();
//        intent.setClass(context, ZhiboActivity.class);
//        intent.putExtra(FLAG_ZHIBO_ROOM_ID, zhiboId);
//        intent.putExtra(FLAG_ZHIBO_ROOM_NAME, zhiboName);
//        intent.putExtra(FLAG_ZHIBO_ROOM_TEACHER_NAME, teacherName);
//        intent.putExtra(FLAG_ZHIBO_ROOM_USER_COUNT, userCount);
//        context.startActivity(intent);
//    }

    public static void startActivity(Context context, String zhiboId, String zhiboName, boolean isClassRoom){
        Intent intent = new Intent();
        intent.setClass(context, ZhiboActivity.class);
        intent.putExtra(FLAG_ZHIBO_ROOM_ID, zhiboId);
        intent.putExtra(FLAG_ZHIBO_ROOM_NAME, zhiboName);
        intent.putExtra(FLAG_ZHIBO_ROOM_CLASS_ROOM, isClassRoom);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mZhiboId = getIntent().getStringExtra(FLAG_ZHIBO_ROOM_ID);
        mZhiboIdCopy = mZhiboId;
        mZhiboname = getIntent().getStringExtra(FLAG_ZHIBO_ROOM_NAME);
        isClassRoom = getIntent().getBooleanExtra(FLAG_ZHIBO_ROOM_CLASS_ROOM, false);
        subjectId = getIntent().getStringExtra(FLAG_ZHIBO_ROOM_SUBJECT_ID);

//        mSubjectDetailBean = getIntent().getParcelableExtra(FLAG_SUBJECT_DETAIL_BEAN);
//        if(mSubjectDetailBean == null){
//            finish();
//        }
        if(isClassRoom){
            zhiboControlView.setShareVisiable(false);
        }
        MyTIMMsgListener.addITIMMsgReceivedCallback(this);
        enterZhiboRoom();
    }

    @Override
    protected void onDestroy() {
        leaveZhiboRoom();
        super.onDestroy();
//        release();
        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.abandonAudioFocus(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    public void enterZhiboRoom(){
        showProgressDialog();
        Call<ZhiboRoomResponse> call;
        if(isClassRoom){
            call = MFPApplication.getInstance().getRestClient().enterTeamZhiboRoom(mZhiboId);//zhiboBean.getId());
        }else{
            call = MFPApplication.getInstance().getRestClient().enterZhiboRoom(mZhiboId);//zhiboBean.getId());
        }
        call.enqueue(new Callback<ZhiboRoomResponse>() {
            @Override
            public void onResponse(Call<ZhiboRoomResponse> call, Response<ZhiboRoomResponse> response) {
                dismissProgressDialog();
                if(response.body().getCode() == 200){
                    mRoomBean = response.body().getData();
                    if(mRoomBean != null){
                        mZhiboImListView.initData(mRoomBean.getTips());
                        startToPlay(mRoomBean.getLivertmp());//"rtmp://kls-live-rtmp.molyfun.com/live/LIVELM16067307416");
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                for(JsonObject jsonObject : mRoomBean.getEvents()){
                                    EventHandler.handlerEvent(jsonObject.toString(), ZhiboActivity.this);
                                }
                            }
                        }, 2000);
                    }
                    if(isClassRoom){
                        mCourserelid = mRoomBean.getCourserelid();
                        mZhiboId = mRoomBean.getCourserelid();
                        zhiboControlView.initData(conversation, ZhiboActivity.this, mCourserelid, mZhiboname, myConnectionListener);
                    }else{
                        zhiboControlView.initData(conversation, ZhiboActivity.this, mZhiboId, mZhiboname, myConnectionListener);
                    }
                }else{
                    return;
                }
            }

            @Override
            public void onFailure(Call<ZhiboRoomResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    public void leaveZhiboRoom(){
        Call<BaseResponse> call;
        if(isClassRoom){
            call = MFPApplication.getInstance().getRestClient().leaveZhiboRoom(mCourserelid);//zhiboBean.getId());
        }else{
            call = MFPApplication.getInstance().getRestClient().leaveZhiboRoom(mZhiboId);//zhiboBean.getId());
        }
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }


}
